import {
  reqSearchResultByKeyword
} from '../../api/index'

Page({
  data: {
    keyword: '',
    showType: '1',
    directlyList: [],
    categoryL1List: [],
    categoryId: 0,
    upperPrice: -1,
    floorPrice: -1,
    descSorted: false,
  },
  onLoad(options) {
    this.setData({
      keyword: options.keyword
    })
  },
  onShow() {
    this.getSearchResult()
  },
  async getSearchResult() {
    try {
      const {
        keyword,
        categoryId,
        upperPrice,
        floorPrice,
        descSorted
      } = this.data
      this.setData({
        directlyList: [],
        categoryL1List: [],
      })
      const res = await reqSearchResultByKeyword({
        keyword,
        categoryId,
        upperPrice,
        floorPrice,
        descSorted
      })
      console.log(res)
      const directlyList = res.data.directlyList.map(item => ({
        listPicUrl: item.listPicUrl,
        topLogo: item.topLogo,
        preLogo: item.preLogo,
        name: item.name,
        itemTagList: item.itemTagList,
        finalPriceInfoVO: item.finalPriceInfoVO,
        retailPrice: item.retailPrice
      }))
      this.setData({
        directlyList,
        categoryL1List: res.data.categoryL1List,
        floorPrice: '',
        upperPrice: '',
        descSorted: false,
      })
    } catch (error) {
      console.log(error)
      wx.showToast({
        title: '搜索关键字失败',
        icon: 'error'
      })
    }
  },
  toggleShowType(e) {
    this.setData({
      showType: e.target.dataset.num
    })
    console.log(typeof e.target.dataset.num)
  },
  clickTag(e) {
    this.setData({
      categoryId: e.target.dataset.id,
      showType: '1'
    })
    this.getSearchResult()
  },
  changeSort(e) {
    this.setData({
      descSorted: e.target.dataset.sort
    })
  },
  confirm(){
    this.getSearchResult()
    this.setData({showType:'1'})
  },
  cancel(){
    this.setData({
      showType:'1',
      categoryId: 0,
      upperPrice: '',
      floorPrice: '',
      descSorted: false,
    })
  }
})